package com.nowcoder.code.lcof;

import com.algorithm.model.TreeNode;
import com.algorithm.model.TreeNodeService;

public class JZ36 {

    public static void main(String[] args) {
        TreeNode treeNode = TreeNodeService.getTreeByArray(new int[]{10, 6, 14, 4, 8, 12, 16});
        JZ36 jz36 = new JZ36();
        jz36.pre(treeNode);
        System.out.println(jz36.head);
    }

    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null) {
            return null;
        }
        pre(pRootOfTree);
        return head;
    }

    TreeNode now = null;
    TreeNode head = null;

    public void pre(TreeNode node) {
        if (node == null) {
            return;
        }
        TreeNode left = node.left;
        TreeNode right = node.right;
        pre(left);

        if (now == null) {
            now = node;
            head = node;
        } else {
            now.right = node;
            node.left = now;
            now = node;
        }
        pre(right);
    }
}
